// MailSlot_C.cpp : Defines the entry point for the console application.
//fangshenghui@gmail.com 2012-06-20

#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>

HANDLE hSlot;
LPTSTR lpszSlotName = _T("\\\\.\\mailslot\\sample_mailslot");
LPTSTR lpzMessage = _T("Test Message for mailslot ");


int _tmain(int argc, _TCHAR* argv[])
{
	BOOL fResult;
	HANDLE hFile;
	DWORD cbWritten;
	DWORD cbMessage;

	//
	hFile = CreateFile (lpszSlotName,
		GENERIC_WRITE,
		FILE_SHARE_READ,
		(LPSECURITY_ATTRIBUTES)NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		(HANDLE)NULL);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		printf("CreatFile failed with %d.\n", GetLastError());
		return FALSE;
	}

	//
	fResult = WriteFile (hFile,
		lpzMessage,
		(DWORD) (lstrlen(lpzMessage) + 1) * sizeof(TCHAR),
		&cbWritten,
		(LPOVERLAPPED) NULL);
	if (!fResult)
	{
		printf ("WriteFile failed with %d.\n", GetLastError());
		return FALSE;
	}

	printf("Slot written to succesfully %s.\n", lpzMessage);
	printf("Slot written to succesfully .\n");
	CloseHandle(hFile);

	system("pause");
	return 0;
}

